/*
 Copyright (C) 2008  Maurizio Monticelli

 This program is free software; you can redistribute it and/or modify it under the terms of the 
 GNU General Public License as published by the Free Software Foundation; either version 2 of the 
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 Public License for more details.

 You should have received a copy of the GNU General Public License along with this program; if not, write 
 to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 
*/

package ch.justdoit.andriodrpncalc.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.EditText;
import ch.justdoit.andriodrpncalc.model.IModelObserver;
import ch.justdoit.andriodrpncalc.model.Model;
import ch.justdoit.andriodrpncalc.view.buttons.*;

public class MainView extends AbsoluteLayout implements IModelObserver {
	
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		// TODO Auto-generated method stub
//		boolean result;
//		result = super.onKeyDown(keyCode, event);
//		return result;
//	}
//



	protected Model model;
	StackDisplay display;
	
	   
	private List<ISecondFunctionButton> secondButtons;
	private List<IThirdFunctionButton> thirdButtons;
	private List<IAlphaFunctionButton> alphaButtons;
	
	private int promptCursorPosition = 0;
	private EditText prompt;


	public void setModel(Model _model) {
		model = _model;
	}


	public MainView(Context context, Model _model)   {
		super(context);
		model = _model;

		secondButtons = new ArrayList<ISecondFunctionButton>();
		thirdButtons = new ArrayList<IThirdFunctionButton>();
		alphaButtons = new ArrayList<IAlphaFunctionButton>();
		
		initView();
	}

	
	public boolean initMenu(Menu menu) {
		return false;
	}



	

	public void initView() {
		
		model.setView(this);
        
       
        int x = 0;
        int y = 0;
        
        

        display = new StackDisplay(mContext, model);
        
        this.addView(display, 
    			new AbsoluteLayout.LayoutParams(display.WIDTH, display.HEIGHT, x ,y)
    	);
        
        y = y + display.HEIGHT;
        x = 1;
        
        MenuGroup menuGroup = new MenuGroup();
        
        KeyMenuDefaultCommands keyMenu = new KeyMenuDefaultCommands(mContext, model, menuGroup);
        this.addView(keyMenu, 
    			new AbsoluteLayout.LayoutParams(keyMenu.WIDTH, keyMenu.HEIGHT, x ,y)
    	);
        
        KeyMenuMathCommands keyMenuMath = new KeyMenuMathCommands(mContext, model, menuGroup);
        keyMenuMath.setVisibility(View.INVISIBLE);
        this.addView(keyMenuMath, 
    			new AbsoluteLayout.LayoutParams(keyMenuMath.WIDTH, keyMenuMath.HEIGHT, x ,y)
    	);
        
        KeyMenuUnitsCommands keyMenuUnits = new KeyMenuUnitsCommands(mContext, model, menuGroup);
        keyMenuUnits.setVisibility(View.INVISIBLE);
        this.addView(keyMenuUnits, 
    			new AbsoluteLayout.LayoutParams(keyMenuMath.WIDTH, keyMenuMath.HEIGHT, x ,y)
    	);
        
        KeyMenuBaseCommands keyMenuBase = new KeyMenuBaseCommands(mContext, model, menuGroup);
        keyMenuBase.setVisibility(View.INVISIBLE);
        this.addView(keyMenuBase, 
    			new AbsoluteLayout.LayoutParams(keyMenuBase.WIDTH, keyMenuBase.HEIGHT, x ,y)
    	);

        KeyMenuVarCommands keyMenuVar = new KeyMenuVarCommands(mContext, model, menuGroup);
        keyMenuBase.setVisibility(View.INVISIBLE);
        this.addView(keyMenuVar, 
    			new AbsoluteLayout.LayoutParams(keyMenuVar.WIDTH, keyMenuVar.HEIGHT, x ,y)
    	);

        KeyMenuStackCommands keyMenuStack = new KeyMenuStackCommands(mContext, model, menuGroup);
        keyMenuBase.setVisibility(View.INVISIBLE);
        this.addView(keyMenuStack, 
    			new AbsoluteLayout.LayoutParams(keyMenuStack.WIDTH, keyMenuStack.HEIGHT, x ,y)
    	);

        y = y + keyMenu.HEIGHT;
        
        
        KeyMenuSelectCommands keyMenuSelectCommands = new KeyMenuSelectCommands(mContext, model, menuGroup);
        this.addView(keyMenuSelectCommands, 
    			new AbsoluteLayout.LayoutParams(keyMenuSelectCommands.WIDTH, keyMenuSelectCommands.HEIGHT, x ,y)
    	);

        y = y + keyMenuSelectCommands.HEIGHT;
        
        KeyEvaluateCommands keyEleluateCommands = new KeyEvaluateCommands(mContext, model);
        this.addView(keyEleluateCommands, 
    			new AbsoluteLayout.LayoutParams(keyEleluateCommands.WIDTH, keyEleluateCommands.HEIGHT, x ,y)
    	);
        
        y = y + keyEleluateCommands.HEIGHT;
        
        
        KeyBaseCommands keyBaseCommands = new KeyBaseCommands(mContext, model);
        this.addView(keyBaseCommands, 
    			new AbsoluteLayout.LayoutParams(keyBaseCommands.WIDTH, keyBaseCommands.HEIGHT, x ,y)
    	);
        
        y = y + keyBaseCommands.HEIGHT;
        
        KeyControl keyControl = new KeyControl(mContext, model);
        this.addView(keyControl, 
    			new AbsoluteLayout.LayoutParams(keyControl.WIDTH, keyControl.HEIGHT, x ,y)
    	);
        
        x = x + keyControl.WIDTH;
        
        KeySecodaryOp keySecodaryOp = new KeySecodaryOp(mContext, model);
        this.addView(keySecodaryOp, 
    			new AbsoluteLayout.LayoutParams(keySecodaryOp.WIDTH, keySecodaryOp.HEIGHT, x ,y)
    	);
        
        x = x + keySecodaryOp.WIDTH;
        
        KeyNumbers keyPad = new KeyNumbers(mContext, model);
        this.addView(keyPad, 
    			new AbsoluteLayout.LayoutParams(KeyNumbers.WIDTH, KeyNumbers.HEIGHT, x, y)
    	);

        x = x + KeyNumbers.WIDTH;

        KeyBaseOp keyBaseOp = new KeyBaseOp(mContext, model);
        this.addView(keyBaseOp, 
    			new AbsoluteLayout.LayoutParams(KeyBaseOp.WIDTH, KeyBaseOp.HIEGHT, x, y)
    	);
	}
    

	

	@Override
	public void registerSecondButtons(ISecondFunctionButton button) {
		secondButtons.add(button);
	}

	@Override
	public void registerThridButtons(IThirdFunctionButton button) {
		thirdButtons.add(button);
	}
	
	public void registerAlphaButton(IAlphaFunctionButton button) {
		alphaButtons.add(button);
	}

	@Override
	public void notifySecondButtons() {
		for (int i = 0; i < secondButtons.size(); i++) {
			secondButtons.get(i).onSecondFunction();
		}
		for (int i = 0; i < thirdButtons.size(); i++) {
			thirdButtons.get(i).onSecondFunction();
		}
	}

	@Override
	public void notifyThirdButtons() {
		for (int i = 0; i < thirdButtons.size(); i++) {
			thirdButtons.get(i).onThirdFunction();
		}
	}

	@Override
	public void notifyFirstButtons() {
		for (int i = 0; i < secondButtons.size(); i++) {
			secondButtons.get(i).onFirstFunction();
		}
		
		for (int i = 0; i < thirdButtons.size(); i++) {
			thirdButtons.get(i).onFirstFunction();
		}
		
		for (int i = 0; i < alphaButtons.size(); i++) {
			alphaButtons.get(i).onFirstFunction();
		}
	
		
	}


	@Override
	public void notifyAlphaButtons() {
		for (int i = 0; i < alphaButtons.size(); i++) {
			alphaButtons.get(i).onAlphaFunction();
		}
		
	}


	public void movePromptSelection(int i) {

		if (prompt != null) {
			if (prompt.isFocused()) {
				// get actual position of the Cursor
				promptCursorPosition = prompt.getSelectionStart();
				int length = prompt.getText().length();

				if (i == 0) {
					// move right
					if (promptCursorPosition < length) {
						promptCursorPosition++;
					}
				} else if (i == 1) {
					// move left
					if (promptCursorPosition > 0) {
						promptCursorPosition--;
					}
				}
				prompt.setSelection(promptCursorPosition);
			}
		}
	}

	public void getPromptText(String text) {
		
		if (text == "")  {
			// if its empty reset the position
			promptCursorPosition = 0;
		} else if (text.length() < promptCursorPosition) {
			// if the text is shorter than the position 
			promptCursorPosition = text.length();
		}
		
		prompt.setText(text);
		prompt.setSelection(promptCursorPosition);
		return;
	}
	
	


	@Override
	public void setPrompt(EditText _prompt) {
		prompt = _prompt;
		
	}


}
